// pages/my/myIndex/myIndex.js
const App = getApp();
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //tab选择
    currentTab:0,
    page:0,
    hasMoreData:true,
    showBoxList:[],//展柜列表
    stePage:0,
    stehasMoreData:true,
    saltedEggList:[]//咸蛋列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.userId == undefined) {
      this.setData({
        userId:wx.getStorageSync('userId')
      })
      this.getUserInfo(wx.getStorageSync('userId')),
      this.getShowBoxList(wx.getStorageSync('userId'));
      this.getSteBoxList(wx.getStorageSync('userId'))
    }else{
      this.setData({
        userId:options.userId
      })
      this.getUserInfo(options.userId),
      this.getShowBoxList(options.userId);
      this.getSteBoxList(options.userId)
    }
    
  },

  //获取个人信息
  getUserInfo:function (userId){
    utils.requestUrl({
      url:'/niu/api/weibo_getuserinfo.php',
      params: {
        markid:wx.getStorageSync('markId'),
        userid:userId
      }
    }).then((res) =>{
      console.log(res.data);
      let data = res.data;
      var chenghaoStr = (data.chenghao).substring(3);
      this.setData({
        faceurl:data.faceurl,
        sex:data.sex,
        ismine:data.ismine,
        nick:data.nick,
        chenghaoStr:chenghaoStr,
        uservip:data.uservip,
        blackcount:data.blackcount,
        set2:data.set2
      })
    })
  },
  //获取展柜列表
  getShowBoxList:function (userId) {
    let that = this;
    let page = that.data.page
    utils.requestUrl({
      url:'/niu/apiv3/niu_getmytoybookv2.php',
      params:{
        markid:wx.getStorageSync('markId'),
        userid:userId,
        page:page
      }
    }).then((res) =>{
      console.log(res);
      var contentListTem = that.data.showBoxList;
        if (res.data.datacount == 0 || res.data.datacount == null) {
          that.setData({
            noContent:true
          })
        }else{
          that.setData({
            noContent:false
          })
        }
        if (res.data.datacount > 0) {
          var contentList = res.data.data
          
          if(contentList == null){
            that.setData({
              noContent:true
            })
            return
          }
          
          if (contentList.length < res.data.pagesize) {
            that.setData({
              showBoxList:contentListTem.concat(contentList),
              hasMoreData: false
            })
          }else{
            that.setData({
              showBoxList: contentListTem.concat(contentList),
              hasMoreData: true,
              page: page + 1
          })
          }
        }
    })
  },
  //获取咸蛋列表
  getSteBoxList:function (userId) {
    let that = this;
    let page = that.data.stePage
    utils.requestUrl({
      url:'/niu/apiv3/xiandan_getmylist.php',
      params:{
        markid:wx.getStorageSync('markId'),
        userid:userId,
        page:page,
        userpage:'1'
      }
    }).then((res) =>{
      console.log(res);
      var contentListTem = that.data.saltedEggList;
        if (res.data.datacount == 0 || res.data.datacount == null) {
          that.setData({
            noContent:true,
            stehasMoreData:false
          })
        }else{
          that.setData({
            noContent:false,
            stehasMoreData:true
          })
        }
        if (res.data.datacount > 0) {
          var contentList = res.data.data
          

          if (contentList.length < res.data.pagesize) {
            that.setData({
              saltedEggList:contentListTem.concat(contentList),
              stehasMoreData: false
            })
          }else{
            that.setData({
              saltedEggList: contentListTem.concat(contentList),
              stehasMoreData: true,
              stePage: page + 1
          })
          }
        }
    })
  },
  //去往详情
  goToDetail:function (e) {
    let id = e.currentTarget.dataset.id
    console.log(id);
    wx.navigateTo({
      url: '/pages/used/usedDetail/usedDetail?id='+id,
    })
  },
  //去往展柜详情
  toShowBoxDetail:function (e){
    let id = e.currentTarget.dataset.id
    console.log(id);
    wx.navigateTo({
      url: '/pages/my/myIndexDetail/myIndexDetail?id='+id,
    })
  },
  //点击切换
  clickTab:function (e) {
    let that = this;
    if (this.data.currentTab == e.currentTarget.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.current,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let userId = this.data.userId;
    if (this.data.hasMoreData) {
      // console.log('触底了')
      this.getShowBoxList(userId);
    }
    
    if (this.data.stehasMoreData) {
      this.getSteBoxList(userId)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var userId = this.data.userId;
    return {
      title: '咸蛋分享',
      path: '/pages/my/myWarehouseShare/myWarehouseShare?userId='+userId,
      imageUrl:'https://api.odancool.com/weixinapp/res/img/common/shareImg.png'
    }
  }
})